<?php
/**
 * Created by PhpStorm.
 * User: sogubaby
 * Date: 2019-02-21
 * Time: 17:00
 */

namespace App\Library\Traits;


use App\Library\Guzzle\Sign;
use GuzzleHttp\Client;

trait Rong360Request {


    public $request_url;

    public $app_id;

    public function __construct() {
        $this->requestUrl = 'https://taojinyundev.rong360.com/gateway';
        $this->appId = '143327234';
    }

    /**
     * 发送请求
     * @param $bizData
     * @param $method
     * @param string $requestMethod
     * @return mixed|\Psr\Http\Message\ResponseInterface
     * @throws \App\Exceptions\GeneralException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @author luwei
     * @date 2019-02-21 17:02
     */
    public function request($bizData, $method,$requestMethod = 'POST') {
        $client = new Client(['base_uri'=>$this->requestUrl,'timeout'=>30.0]);
        $params             = array(
            'method'    => $method,
            'app_id'    => $this->appId,
            'version'   => '1.0',
            'sign_type' => 'RSA',
            'format'    => 'json',
            'timestamp' => time()
        );

        $params['biz_data'] = json_encode($bizData,true);
        $params['sign']    = Sign::signForClient($params);
        ksort($params);
        $result = $client->request($requestMethod,'',['json'=>$params,'verify'=>false]);
        return json_decode($result->getBody()->getContents(), true);
    }

}